package com.smile.ye.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.ye.data.dto.ProductItemCreateDto;
import com.smile.ye.data.dto.ProductItemQueryDto;
import com.smile.ye.data.dto.ProductItemUpdateDto;
import com.smile.ye.data.entity.ProductItemEntity;
import com.smile.ye.mapper.ProductItemMapper;
import com.smile.ye.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Component
@Service
public class ProductItemService {
    @Autowired
    ProductItemMapper mapper;

    public Result query(ProductItemQueryDto dto){
        IPage<ProductItemEntity> page = new Page(dto.getPage(), dto.getSize());
        QueryWrapper<ProductItemEntity> wrapper = new QueryWrapper<>();
        Map<String , String> map = new HashMap<>();
        map.put("menu" , dto.getMenu());
        map.put("status" , dto.getStatus());
        wrapper.allEq(map,false).and(dto.getKeywords()!=null,i -> {
            i.like("title",dto.getKeywords())
                    .and(it -> it.like("code",dto.getKeywords()));
        });
        mapper.selectPage(page,wrapper);
        return Result.build(page);
    }

    public Result queryById(String id){
        ProductItemEntity productItemEntity = mapper.selectById(id);
        return Result.build(productItemEntity);
    }

    public Result create(ProductItemCreateDto dto){
        mapper.insert(dto);
        return Result.build();
    }

    public Result update(String id, ProductItemUpdateDto dto){
        dto.setId(id);
        mapper.updateById(dto);
        return Result.build();
    }

    public Result remove(String id){
        mapper.deleteById(id);
        return Result.build();

    }
}
